package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IMedcineStoreDAO;
import com.trkj.dao.jpa.ISSSendDrugsDetailsDAO;
import com.trkj.dao.jpa.IStaffDAO_zjs;
import com.trkj.dao.jpa.IStorageHasDrugsDAO;
import com.trkj.dao.mybatis.SSSendDrugsDAO;
import com.trkj.pojos.DrugstorageHasdrug;
import com.trkj.pojos.Medcinestore;
import com.trkj.pojos.SendmedcineDrugstore;
import com.trkj.pojos.SendmedcineDrugstoreDetail;
import com.trkj.pojos.Staff;

@Service
@Transactional
public class SSSendDrugsService {
	@Autowired
	private SSSendDrugsDAO ssSendDrugsDAO;
	@Autowired
	private IMedcineStoreDAO medcineStoreDAO;
	@Autowired
	private IStaffDAO_zjs staffDAO_zjs;
	@Autowired
	private IStorageHasDrugsDAO storageHasDrugsDAO;
	@Autowired
	private ISSSendDrugsDetailsDAO issSendDrugsDetailsDAO;
	/**
	 * 药库发药成功
	 */
	public void SSsendDrugsYes(String SSsendDrugsPeople,Long sendmedcineDetailsId,BigDecimal medcineNumber,String medcineName) {
		DrugstorageHasdrug drug = storageHasDrugsDAO.findDrugstorageHasdrugByName(medcineName);
		System.out.println("---------------"+drug);
		BigDecimal newMedcineNumber = drug.getMedcineNumber().subtract(medcineNumber);
		drug.setMedcineNumber(newMedcineNumber);
		System.out.println("+++++++++"+drug);
		issSendDrugsDetailsDAO.findOne(sendmedcineDetailsId).setStaff(staffDAO_zjs.findOne(SSsendDrugsPeople));
	}
	
	/**
	 * 查询所有的操作人信息
	 * @return
	 */
	public List<Staff> findAllStaff(){
		return staffDAO_zjs.findAllStaff();
	}
	/**
	 * 查询所有的药房信息
	 * @return
	 */
	public List<Medcinestore> findAllMedcineStore(){
		return medcineStoreDAO.findAllMedcineStore();
	}
	/**
	 * 药库发药信息
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllSSSendDrugs(int page,int rows){
		Page<SendmedcineDrugstore> drugstores=PageHelper.startPage(page, rows,true);
		List<SendmedcineDrugstore> sendmedcineDrugstores = ssSendDrugsDAO.getAllSsSendDrugs();
		System.out.println("sendmedcineDrugstores==="+sendmedcineDrugstores.size());
		Map<String,Object> map = new HashMap<>();
		map.put("total", drugstores.getTotal());
		map.put("rows", sendmedcineDrugstores);
		return map;
	}
	
	/**
	 * 按条件分页查询药库发药信息
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map getSomeSSSendDrugs(int page,int rows,Map map) {
		Page<SendmedcineDrugstore> drugstores=PageHelper.startPage(page, rows,true);
		List<SendmedcineDrugstore> sendmedcineDrugstores = ssSendDrugsDAO.getSomeSSsendDrugs(map);
		Map map1 = new HashMap<>();
		map1.put("total", drugstores.getTotal());
		map1.put("rows", sendmedcineDrugstores);
		return map1;
	}
	
	/**
	 * 按药品名称查询
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map searchSSsendDrugsByName(int page,int rows ,Map map) {
		Page<SendmedcineDrugstore> drugstores=PageHelper.startPage(page, rows,true);
		List<SendmedcineDrugstore> sendmedcineDrugstores = ssSendDrugsDAO.getSomeSSsendDrugsByName(map);
		Map map1= new HashMap<>();
		map1.put("total", drugstores.getTotal());
		map1.put("rows", sendmedcineDrugstores);
		return map1;
		
	}
	/**
	 * 获取药库所有的发药详情Map
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllSSsendDrugsDetailsMap(int page,int rows,Map map5) {
		Page<SendmedcineDrugstoreDetail> details = PageHelper.startPage(page, rows,true);
		List<SendmedcineDrugstoreDetail> details2 =ssSendDrugsDAO.getAllSSsendDrugsDetail(map5);
		List details3 = new ArrayList<>();
		if(!details2.isEmpty()) {
			String a = JSON.toJSONString(details2);
			JSONArray arr = JSON.parseArray(a);
			for(int i=0;i<arr.size();i++) {
				Map map =new HashMap<>();
				JSONObject obj = arr.getJSONObject(i);
				JSONObject medcineDetail = obj.getJSONObject("medcineDetail");
				String medcineName = medcineDetail.getString("medcineName");
				JSONObject medcinestore = obj.getJSONObject("medcinestore");
				String medcinestoreName = medcinestore.getString("medcinestoreName");
				JSONObject sendmedcineDrugstore = obj.getJSONObject("sendmedcineDrugstore");
				String sendmedcineName = sendmedcineDrugstore.getString("sendmedcineName");
				//JSONObject staff = obj.getJSONObject("staff");
				String sendmedcineNumber = obj.getString("sendmedcineNumber");
				String sendmedcineDetailsId = obj.getString("sendmedcineDetailsId");
				map.put("sendmedcineDetailsId", sendmedcineDetailsId);
				map.put("sendmedcineNumber", sendmedcineNumber);
				String staffname = "";
				if(obj.get("staff")!=null) {
					JSONObject staff = obj.getJSONObject("staff");
					 staffname = staff.getString("staffname");
				}
				map.put("staffname", staffname);
				map.put("sendmedcineName", sendmedcineName);
				map.put("medcinestoreName", medcinestoreName);
				map.put("medcineName", medcineName);
				details3.add(map);
			}
		}
		Map map1 = new HashMap<>();
		map1.put("total", details.getTotal());
		map1.put("rows", details3);
		return map1;
	}
}
